package com.qs.eggs.view.ordermanager.fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.OrderAdapter;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.widgets.custom.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class WaitEvaluationFragment extends BaseFragment implements Runnable,PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener{
    private PullToRefreshView mPullToRefreshView;
    private RecyclerView recycleView;
    private List<Map<String, Object>> orderlist= null;
    private Handler mHandler;
    private OrderAdapter orderAdapter;
    private TextView tishi;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        Thread thread = new Thread(this);
        thread.start();
        recycleView=findView(R.id.recyclerview);
        tishi=findView(R.id.tishi);
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mPullToRefreshView =findView(R.id.main_pull_refresh_view);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        getData();
    }
    //获得全部订单
    private void getData(){
        HashMap<String,String> map=new HashMap<>();
        map.put("orderstatus","3");
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=searchMyOrder")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("全部订单=="+s);
                        try {
                            orderlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            if (jsonArray.length()==0){
                                tishi.setVisibility(View.VISIBLE);
                            }else{
                                tishi.setVisibility(View.GONE);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                Map<String, Object> map = new HashMap<>();
                                map.put("id", jsonObject.getString("id"));
                                map.put("orderno", jsonObject.getString("orderno"));
                                map.put("createtime", jsonObject.getString("createtime"));
                                map.put("orderstatus", jsonObject.getString("orderstatus"));
                                map.put("totalcount", jsonObject.getString("totalcount"));
                                map.put("totalmoney", jsonObject.getString("totalmoney"));
                                orderlist.add(map);
                            }
                            }
                            orderAdapter = new OrderAdapter(getActivity(), orderlist,mHandler);
                            recycleView.setAdapter(orderAdapter);

                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void run() {
        Looper.prepare();
        mHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case 5:
                        getData();
                        break;
                    default:
                        break;
                }
            }
        };
        Looper.loop();
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler1.sendEmptyMessage(1);
                getData();
                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 500);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler1.sendEmptyMessage(1);
                getData();
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 500);

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler1 = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }

    };
    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}